package com.ifacebox.face.track.ui;

import com.ifacebox.face.track.model.FaceConfig;
import com.ifacebox.face.track.sevice.FaceConfigService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

/**
 * @author znn
 */
public class FaceConfigDialog extends JDialog implements ActionListener {
    private static final ImageIcon SAVE = new ImageIcon(Objects.requireNonNull(FaceConfigDialog.class.getResource("/save.png")));
    private JTextField  appId, sdkKey;
    private JSpinner faceScore;
    private JButton save;
    private final FaceConfigService faceConfigService;

    public FaceConfigDialog(JFrame owner) {
        super(owner, owner.getTitle(), true);
        super.setIconImage(owner.getIconImage());
        super.setLayout(new BorderLayout());
        super.add(new JToolBar() {{
            this.setFloatable(false);
            this.setLayout(new FlowLayout(FlowLayout.LEFT));
            this.add(save = new JButton(SAVE) {{
                this.addActionListener(FaceConfigDialog.this);
            }});
        }}, BorderLayout.NORTH);
        super.add(new Box(BoxLayout.Y_AXIS) {{
            this.add(new Box(BoxLayout.X_AXIS) {{
                this.add(new JLabel("应用编码："));
                this.add(appId = new JTextField("", 32));
                this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            }});
            this.add(new Box(BoxLayout.X_AXIS) {{
                this.add(new JLabel("算法密钥："));
                this.add(sdkKey = new JTextField("", 32));
                this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            }});
            this.add(new Box(BoxLayout.X_AXIS) {{
                this.add(new JLabel("人脸阈值："));
                this.add(faceScore = new JSpinner(new SpinnerNumberModel(80.00, 0.00, 100.00, 1.00)));
                this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            }});
            this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        }}, BorderLayout.CENTER);
        super.pack();
        super.setResizable(false);
        super.setLocationRelativeTo(null);
        this.faceConfigService = new FaceConfigService();
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            try {
                FaceConfig faceConfig = this.faceConfigService.readConfig();
                this.faceScore.setValue(faceConfig.getFaceScore());
                this.appId.setText(faceConfig.getAppId());
                this.sdkKey.setText(faceConfig.getSdkKey());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
            }
        }
        super.setVisible(b);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.save) {
            try {
                FaceConfig faceConfig = this.faceConfigService.readConfig();
                faceConfig.setFaceScore((double)this.faceScore.getValue());
                faceConfig.setAppId(appId.getText());
                faceConfig.setSdkKey(sdkKey.getText());
                this.faceConfigService.writeConfig(faceConfig);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
